package com.cskaoyan.controller;

import com.cskaoyan.bean.promotion.Ad;
import com.cskaoyan.service.promotion.AdService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.promotion.AdListVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @autyor:caicaicai
 */
@RestController
public class AdController {

    @Autowired
    AdService adService;

    @RequestMapping("admin/ad/list")//page=1&limit=20&sort=add_time&order=desc
    @RequiresPermissions("admin:ad:list")
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, String name, String content){
        AdListVo adListVo = adService.query(page, limit, sort, order, name, content);
        return BaseRespVo.ok(adListVo);
    }

    @RequestMapping("admin/ad/update")
    @RequiresPermissions("admin:ad:update")
    public BaseRespVo update(@RequestBody Ad ad){
        int code = adService.update(ad);
        if(code != 200  ){
           /* return BaseRespVo.fail("编辑失败！");*/
            return BaseRespVo.error(402,"编辑失败！");
        }
        return BaseRespVo.ok(ad);
    }

    @RequestMapping("/admin/ad/create")
    @RequiresPermissions("admin:ad:create")
    public BaseRespVo create(@RequestBody Ad ad){
        Ad adVo = adService.create(ad);
        return BaseRespVo.ok(adVo);
    }

    @RequestMapping("admin/ad/delete")
    @RequiresPermissions("admin:ad:delete")
    public BaseRespVo delete(@RequestBody Ad ad){
        int code = adService.delete(ad);
        if(code != 200  ){
            //return BaseRespVo.fail("删除失败！");
            return BaseRespVo.error(402,"删除失败！");
        }
        return BaseRespVo.ok();
    }

}
